//
//  LJFVLoginOrRegisterController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/25.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFVLoginOrRegisterController.h"
#import "LJFRegisterController.h"
#import "LJFHttpManager.h"
#import "Connector.h"

@interface LJFVLoginOrRegisterController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *name;

@property (weak, nonatomic) IBOutlet UITextField *passWord;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@end

@implementation LJFVLoginOrRegisterController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setbuttonCorneradius];
    
    [self addtextFiledNotification];
}

- (void)setbuttonCorneradius
{
    _loginButton.layer.cornerRadius = 10;
    _registerButton.layer.cornerRadius = 10;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (IBAction)login:(UIButton *)sender
{
    [self.view endEditing:YES];
    
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    NSString *urlStr = [NSString stringWithFormat:URL_LOGIN_parmater ,_name.text,_passWord.text];
    
    [manager getDataFromNetWithUrl:URL_LOGIN AndParameterStr:urlStr success:^(id responseObj)
    {
        
        NSNumber *num = responseObj[@"status"][@"succeed"];
        
        int succeed = [num intValue];
        
        if (succeed == 0)
        {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:responseObj[@"status"][@"error_desc"] delegate: self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alter show];
            
        }else if (succeed == 1)
        {
            [self writeNameAndPassWord];
            
            [_mydelegate LoginSuccess:self successDictionary:responseObj[@"data"]];
            
            [self back:nil];
        }

    } failure:^(NSError *error)
    {
        NSLog(@"error == %@",error);
        
    }];
}

- (void)writeNameAndPassWord
{
    NSUserDefaults *mydefaults = [NSUserDefaults standardUserDefaults];
    [mydefaults setObject:_name.text forKey:@"name"];
    [mydefaults setObject:_passWord.text forKey:@"passWord"];
    [mydefaults setObject:@"YES" forKey:@"isLogin"];
    [mydefaults synchronize];
}

- (IBAction)myregister:(id)sender
{
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.4;
//    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:animation forKey:nil];
    
    
    LJFRegisterController *regController = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFRegisterController"];
    
    [self presentViewController:regController animated:YES completion:nil];

}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - 添加textfiled观察者

- (void)addtextFiledNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 观察textfiled状态,改变登陆和注册按钮状态

- (void)textDidChange
{
    if (_name.text.length>0 && _passWord.text.length>0)
    {
        _loginButton.alpha = 1.0;
        
        _loginButton.enabled = YES;
    }
    else
    {
        _loginButton.alpha = 0.5;
        
        _loginButton.enabled = NO;
    }

}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}


#pragma mark - 返回上一个控制器

- (IBAction)back:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
